package com.emm.yixun.system.service.impl.manage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.AppPushService;
import com.emm.yixun.basic.service.MessageReceiverService;
import com.emm.yixun.basic.service.NoticeService;
import com.emm.yixun.common.app.base.CommonResDTO;
import com.emm.yixun.common.model.dto.MessageReceiverDto;
import com.emm.yixun.common.model.dto.NoticeDto;
import com.emm.yixun.common.model.enums.IsSendEnum;

@Service("customerNoSignedPushMsgCurrManage")
public class CustomerNoSignedPushMsgCurrManage {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private MessageReceiverService messageReceiverService;
	@Autowired
	private AppPushService appPushService;
	@Autowired
	private NoticeService noticeService;

	/**
	 * 推送逾期未签约消息
	 * 
	 * @param reqDto
	 * @return
	 */
	public CommonResDTO pushMsg() {
		CommonResDTO resDto = new CommonResDTO();
		resDto.setResult("0");
		try {
			// 查询逾期未签约相关推送消息
			List<MessageReceiverDto> msgRecList = new ArrayList<MessageReceiverDto>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar calendar = Calendar.getInstance();
			int year = calendar.get(Calendar.YEAR);
			int month = calendar.get(Calendar.MONTH) + 1;
			int day = calendar.get(Calendar.DAY_OF_MONTH);
			String timerNoticeBefore = year + "-" + month + "-" + day + " 00:00:00";
			String timerNoticeAfter = year + "-" + month + "-" + day + " 23:59:59";
			// 查询逾期未签约推送时间在当前的消息
			MessageReceiverDto mrDto = new MessageReceiverDto();
			mrDto.setNoticeTempType("NT01");
			mrDto.setIsSend(IsSendEnum.UNSEND.getNo());
			mrDto.setTimerNoticeBefore(sdf.parse(timerNoticeBefore));
			mrDto.setTimerNoticeAfter(sdf.parse(timerNoticeAfter));
			List<MessageReceiverDto> mrBirthList = messageReceiverService.selectPushMsgListByDto(mrDto);
			if (mrBirthList != null) {
				for (MessageReceiverDto mrBirth : mrBirthList) {
					msgRecList.add(mrBirth);
				}
			}

			if (msgRecList != null && msgRecList.size() > 0) {
				String noticeIds = "";
				String msgRecIds = "";
				Set<String> noticeIdSet = new HashSet<String>();
				for (MessageReceiverDto msgRecDto : msgRecList) {
					// 开始推送消息
					Map<String, String> pushMap = new HashMap<String, String>();
					pushMap.put("action", "逾期未签约提醒");
					pushMap.put("remindCategoryId", String.valueOf(msgRecDto.getDictionaryId()));
					pushMap.put("pushType", "YQWQYTX");
					String context = msgRecDto.getContent();
					appPushService.pushMessage(String.valueOf(msgRecDto.getReceiverId()), context,
							msgRecDto.getTitle(), pushMap);

					// 消息ID,Set
					noticeIdSet.add(String.valueOf(msgRecDto.getNoticeId()));
					// 消息接收人ID'S
					if (StringUtils.isNotEmpty(msgRecIds)) {
						msgRecIds += ",";
					}
					msgRecIds += String.valueOf(msgRecDto.getId());
				}
				// 需更新消息ID'S
				if (!noticeIdSet.isEmpty()) {
					for (String noticeId : noticeIdSet) {
						if (StringUtils.isNotEmpty(noticeIds)) {
							noticeIds += ",";
						}
						noticeIds += noticeId;
					}
				}
				// 更新消息
				NoticeDto noticeDto = new NoticeDto();
				noticeDto.setIsSend(IsSendEnum.SEND.getNo());
				noticeService.updates(noticeDto, noticeIds);
				// 更新接收人
				MessageReceiverDto updateMRDto = new MessageReceiverDto();
				updateMRDto.setIsSend(IsSendEnum.SEND.getNo());
				messageReceiverService.updates(updateMRDto, msgRecIds);
			}
			resDto.setResult("1");
		} catch (Exception e) {
			logger.error("推送逾期未签约提醒消息失败: ", e);
			resDto.setErrorCode("00000");
			resDto.setErrorMsg("推送逾期未签约提醒消息失败!");
			resDto.setResult("0");
			return resDto;
		}
		return resDto;
	}

}
